﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Com.Qkds.Erp.WebUi
{
    public partial class orderslist : System.Web.UI.Page
    {
        public int pid
        {
            get
            {
                return int.Parse(ViewState["pid"].ToString());
            }
            set
            {
                ViewState["pid"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pid = 0;
                if (Request["id"] != null)
                {
                    string TableName = "orders";
                    pid = int.Parse(Request["id"].ToString());
                    OrdersModel model = new OrdersBLL().GetModelByID(TableName, pid);
                    this.txtTime.Value = model.Time.ToString();
                    this.txtPrice.Value = model.Price.ToString();
                    this.txtEmployee.Value = model.EmployeeID.ToString();
                    this.txtVendor.Value = model.VendorID.ToString();
                    this.txtAB.Value = model.AB.ToString();
                    this.txtRemark.Value = model.Remark;
                }

            }
        }
        protected void btnSave_click(object sender, EventArgs e)
        {
            DataTable a = new FundsBLL().A();
            decimal aa = decimal.Parse(a.Rows[0]["EMoney"].ToString());
            decimal b = decimal.Parse(this.txtPrice.Value);
            OrdersModel model = new OrdersModel()
            {
                Time =DateTime.Parse (this.txtTime.Value),
                Price = decimal.Parse(this.txtPrice.Value),
                EmployeeID = int.Parse(this.txtEmployee.Value),
                VendorID = int.Parse(this.txtVendor.Value),
                AB = int.Parse(this.txtAB.Value),
                Remark = this.txtRemark.Value
            };
            DataTable w = new OrdersBLL().A();
            int ww = int.Parse(w.Rows[0]["ID"].ToString());
            FundsModel fundsModel = new FundsModel()
            {
                OrdersID = ww+1,
                DMoney = b,
                // EMoney=
                EMoney = aa - b
            };
            
            OrdersBLL bll = new OrdersBLL();
            if (pid > 0)
            {
                bll.Gai(model, pid);

            }
            else
            {
                bll.Add(model);


            }
            FundsBLL funds = new FundsBLL();
            funds.Add(fundsModel);
            Response.Redirect("orders.aspx");
        }
    }
}